#include "color.h"

#include <math.h>

flip_color flip_hsv(double h, double s, double v)
{
	double hs = fmod(h, 360.0) / 60.0;
	double hsi = floor(hs);
	int hi = (int)hsi % 6;
	double f = hs - hsi;
	
	int vi = (int)(v * 255.0);
	
	int p = (int)(v * (1.0 - s) * 255.0);
	int q = (int)(v * (1.0 - f * s) * 255.0);
	int t = (int)(v * (1.0 - (1.0 - f) * s) * 255.0);
	
	switch(hi)
	{
		case 0: return flip_rgba(vi, t, p, 0);
		case 1: return flip_rgba(q, vi, p, 0);
		case 2: return flip_rgba(p, vi, t, 0);
		case 3: return flip_rgba(p, q, vi, 0);
		case 4: return flip_rgba(t, p, vi, 0);
		case 5: return flip_rgba(vi, p, q, 0);
	}
	
	return 0; // Will never be reached
}
